/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.solarchallenge.server.persistence.datastore;

import com.google.solarchallenge.server.domain.UserAccountDao;
import com.google.solarchallenge.server.persistence.PMF;
import com.google.solarchallenge.server.persistence.UserAccountPersistenceInterface;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

/**
 * Implementation for {@link UserAccountPersistenceInterface}.
 *
 * @author Arjun Satyapal
 */
public class UserAccountDataStore implements UserAccountPersistenceInterface{
  @Override
  public UserAccountDao getUserAccountDaoByEmail(String email) {
    PersistenceManager pm = PMF.getPersistenceManager();
    Query query = pm.newQuery(UserAccountDao.class);
    query.setFilter("email == :em");
    query.setUnique(true);

    try {
      UserAccountDao dao = (UserAccountDao) query.execute(email);
      if (dao != null) {
        return dao;
      }
    } catch (Throwable caught) {
      // TODO(arjuns) : Handle exception.
      caught.printStackTrace();
    } finally {
      query.closeAll();
      pm.close();
    }

    return null;
  }
}
